package com.minning.protocol;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/* 
* 类名称　: 
* 功能描述: 
* 修订历史: 
* 版本 日期  作者 描述
* 2016年2月25日 张奇 
*/
public class ProtocolGetFromDb {

	/**
	 * 
	 * @param conn
	 * @return 信号列表
	 */
	public static Map<String, SignalsBean> selectFromSignals(Connection conn) {

		Map<String, SignalsBean> signallistMap = new HashMap<String, SignalsBean>();
		try {
			String sql = "select * from signals";
			PreparedStatement pst = conn.prepareStatement(sql);
			ResultSet ret = pst.executeQuery();
			while (ret.next()) {
				SignalsBean signalBean = new SignalsBean();
				signalBean.setId(Integer.parseInt(ret.getString("Id")));
				signalBean.setSignalType(ret.getString("signalType"));
				signalBean.setDataType(ret.getString("dataType"));
				signalBean.setSignalName(ret.getString("signalName"));
				signalBean.setSignalCNName(ret.getString("signalCNName"));
				signalBean.setStartAddr(Integer.parseInt(ret.getString("startAddr")));
				signalBean.setBigLittleEndian(ret.getString("bigLittleEndian"));
				signalBean.setWeight(ret.getString("weight"));
				signalBean.setUnit(ret.getString("unit"));
				signalBean.setMaximum(ret.getString("maximum"));
				signalBean.setMininum(ret.getString("mininum"));
				// Signallist.add(signalBean);

				signallistMap.put(ret.getString("Id"), signalBean);
			}
			ret.close();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 显示数据

		return signallistMap;
	}

	/**
	 * 
	 * @param conn
	 * @param startAddr
	 *            startAddress
	 * @return 指定startAddress开始的协议列表
	 */
	public static List<ProtocolBean> selectFromProtocol(Connection conn, int startAddr) {

		List<ProtocolBean> protocolBeanList = new ArrayList<>();
		try {
			String sql = "select * from protocol where startAddr >= " + startAddr + " order by startAddr";
			PreparedStatement pst = conn.prepareStatement(sql);
			ResultSet ret = pst.executeQuery();
			while (ret.next()) {
				ProtocolBean protocolBean = new ProtocolBean();
				protocolBean.setId(Integer.parseInt(ret.getString("Id")));
				protocolBean.setItemId(Integer.parseInt(ret.getString("itemId")));
				protocolBean.setStartAddr(Integer.parseInt(ret.getString("startAddr")));
				protocolBean.setItemType(ret.getString("itemType"));
				protocolBeanList.add(protocolBean);
			}
			ret.close();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 显示数据

		return protocolBeanList;
	}

	/**
	 * 
	 * @param conn
	 * @param booleanset
	 *            布尔集合类型
	 * @return 指定booleansetid 的信号列表
	 */
	public static Map<String, List<BooleanItemBean>> selectFromBooleansignal(Connection conn, String booleanset) {

		Map<String, List<BooleanItemBean>> booleanSignalMap = new HashMap<String, List<BooleanItemBean>>();
		List<BooleanItemBean> booleanItemBeanList = new ArrayList<>();
		try {
			String sql = "SELECT itemId as itemId, booleansignal.signalId as signalId, booleansignal.startBit as startBit FROM ( SELECT * FROM protocol WHERE itemType = '"
					+ booleanset
					+ "' ) a JOIN booleanset ON a.itemId = booleanset.id JOIN booleansignal ON booleanset.booleansetid = booleansignal.booleansetid ORDER BY itemId";
			
			
			
			System.out.println(sql);
			PreparedStatement pst = conn.prepareStatement(sql);
			ResultSet ret = pst.executeQuery();
			String olditemId = null;
			String newitemId = null;
			while (ret.next()) {
				newitemId=ret.getString("itemId");
				if (booleanItemBeanList.size() > 1 && !newitemId.equals(olditemId)) {
					booleanSignalMap.put(olditemId,booleanItemBeanList);
					booleanItemBeanList = new ArrayList<>();
				}
				BooleanItemBean booleanItemBean = new BooleanItemBean();
				booleanItemBean.setItemId(Integer.parseInt(ret.getString("itemId")));
				booleanItemBean.setSignalId(Integer.parseInt(ret.getString("signalId")));
				booleanItemBean.setStartBit(Integer.parseInt(ret.getString("startBit")));
				booleanItemBeanList.add(booleanItemBean);
				olditemId = ret.getString("itemId");
			}
			booleanSignalMap.put(olditemId,	booleanItemBeanList);
			ret.close();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 显示数据

		return booleanSignalMap;
	}

	/**
	 * 
	 * @param conn
	 * @param booleanset
	 *            布尔集合类型
	 * @return 指定booleansetid 的信号列表
	 */
	public static Map<String, Map<String, SignalEnumBean>> selectFromSignalEnum(Connection conn) {

		Map<String, Map<String, SignalEnumBean>> signalEnumBeanMap = new HashMap<String, Map<String, SignalEnumBean>>();
		try {
			String sql = "select * from signalEnum order by signalId";
			PreparedStatement pst = conn.prepareStatement(sql);
			ResultSet ret = pst.executeQuery();
			String oldsignalid = null;
			String newsignalid = null;
			Map<String, SignalEnumBean> signalEnumBeanList = new HashMap<String, SignalEnumBean>();
			while (ret.next()) {
				newsignalid=ret.getString("signalId");
				if (signalEnumBeanList.size() > 1 && !newsignalid.equals(oldsignalid)) {
					signalEnumBeanMap.put(oldsignalid, signalEnumBeanList);
					signalEnumBeanList = new HashMap<String, SignalEnumBean>();
				}
				SignalEnumBean signalEnumBean = new SignalEnumBean();
				signalEnumBean.setId(Integer.parseInt(ret.getString("Id")));
				signalEnumBean.setSignalId(Integer.parseInt(newsignalid));
				signalEnumBean.setKey(ret.getString("key"));
				signalEnumBean.setValue(ret.getString("value"));
				signalEnumBeanList.put(ret.getString("key"), signalEnumBean);
				oldsignalid = ret.getString("signalId");
			}
			signalEnumBeanMap.put(oldsignalid, signalEnumBeanList);
			ret.close();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 显示数据

		return signalEnumBeanMap;
	}
}
